<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Pray as PrayModel;
use app\common\model\Practice as PracticeModel;

/**
 * Pray 忏悔祈福控制器
 * Class Pray
 * @package app\home\controller
 */
class Pray extends Base
{
    /**
     * [getIndex  获取评论]
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new PrayModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            $map['type'] = $param['type'];
            if(empty($param['is_all'])){
                $map['memid'] = $user['id'];
            }else{
                $map['create_time'] = ['gt',1596589866];
                $map['is_ni'] = ['in','2,3'];
            }

            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size,'am.img,am.username,p.content,p.create_time,p.is_ni');
            foreach($lists as &$v){
                if($v['is_ni'] == 2 || $v['is_ni'] == 0){
                    $http = substr($v['img'],0,4);
                    if($http != 'http') {
                        $v['img'] = config('domain_name') . $v['img'];
                    }
                }else{
                    $v['img'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/niming.jpg';
                    $v['username'] = '匿名';
                }
                $v['addtime'] = MessageTime(strtotime($v['create_time']));
                $v['content'] = urldecode( $v['content']);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  添加祈福和忏悔]
     * @param   type 1祈福 2忏悔
     * @return json
     * @author [C。]
     */
    public function postAdd()
    {
        $model = new PrayModel;
        $pmodel = new PracticeModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $param['memid'] = $user['id'];
            $param['content'] = urlencode( $param['content']);
            $model->addData($param);
            $practice = $pmodel->getRound(['type'=>3],'content');
            if($param['type'] == 1){
                add_quality($user['id'],16,$user['quality'],2);
            }else{
                add_quality($user['id'],17,$user['quality'],2);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '提交失败'.$e->getMessage());
        }
        return admin_json(1, '提交成功',$practice['content']);
    }
}